<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\business\BusinessBackpigFinish;
use kartik\file\FileInput;
use kartik\select2\Select2;
use yii\web\JsExpression;
use backend\assets\PrintAsset;
PrintAsset::register($this);



//购买总费用
$buytotal = 0;
//回购总费用
$backtotal = 0;
//总体养殖成本
$costtotal = 0;
/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessBackpigFinish */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <div class="adv-table editable-table ">
                <div class="business-backpig-finish-form">
                    <?php $form = ActiveForm::begin([
                        'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                        'fieldConfig' => [
                            'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                        ]
                    ]); ?>


                    <div class="form-group">
                        <div class="col-xs-3 col-sm-2 text-right">
                            <label class="control-label" for="businessbackpigfinish-totalbackprice">结算的回购单</label>
                        </div>
                        <div class="col-xs-9 col-sm-7">
                            <?php foreach($allorder as $key => $row) { ?>
                            <?= Html::label($row['number'],'',['class'=>'btn btn-xs btn-default']) ?>
                            <?php } ?>
                        </div>
                        <div class="col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0">
                            <div class="help-block"></div>
                        </div>
                    </div>



                    <?= $form->field($model, 'number')->textInput(['readonly'=>true,'value'=>$model->isNewRecord?date('YmdHis').rand(100,999):$model->number]) ?>

                    <?= $form->field($model, 'totalbackprice')->textInput(['maxlength' => true,'value' => $backprice]) ?>

                    <?= $form->field($model, 'finishprice')->textInput(['maxlength' => true]) ?>

                    <?= $form->field($model, 'finishuser')->textInput(['maxlength' => true,'value'=>Yii::$app->user->identity->username,'readonly'=>true]) ?>

                    <?= $form->field($model, 'status')->dropDownList(BusinessBackpigFinish::$statues) ?>

                    <?= $form->field($model, 'remark')->textarea(['maxlength' => true]) ?>



                    <input type="hidden" name="backids" value="<?= Yii::$app->request->get('ids') ?>">
                    <div class="form-group">
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary','style'=>'margin-left:100px;']) ?>
                    </div>
                    <?php ActiveForm::end(); ?>
                </div>
            </div>
        </div>
    </section>
</div>
</div>

<!--打印区域-->
<div class="row" style="background: #fff"><!--打印结算明细区域-->
    <div id="printarae" class="col-md-10 col-md-offset-1">

        <h4 style="margin-bottom: 10px;text-align: center">明细结算单</h4>

        <div class="col-md-8 col-md-offset-2">
            <span style="width: 50%;display: block;float: left;"><b>农户:<?= $print[0]['farmername'] ?></b></span>
            <span style="width: 125px;display: block;float: right;"><b>时间：<?= date('Y-m-d') ?></b></span>
        </div>
        <div class="col-md-8 col-md-offset-2">
            <?php
            foreach ($print as $key => $row) {
            $buytotal += $row['buycost'];
            $backtotal += $row['backprice'];
            ?>
            <table class="table table-bordered">
                <tr>
                    <th>仔猪编号</th>
                    <th>购买费用</th>
                    <th>回收价格</th>
                    <th>回购单审核人</th>
                </tr>

                <tr>
                    <td><?= $row['pigno'] ?></td>
                    <td><?= $row['buycost'] ?></td>
                    <td><?= $row['backprice'] ?></td>
                    <td><?= $row['agreeuser'] ?></td>
                </tr>
                <table class="table table-bordered">
                    <tr>
                        <th>成本</th>
                    </tr>
                    <tr>
                        <td>
                            <table class="table" style="background-color: rgba(0, 0, 0, 0)">
                                <tr>
                                    <th>成本类型</th>
                                    <th>价格</th>
                                    <th>备注</th>
                                </tr>
                                <?php
                                $costdata = unserialize($row['cost']);
                                $total = 0;
                                foreach ($costdata as $k => $v) {
                                    $total += $v['price'];
                                    $costtotal += $v['price'];
                                    ?>
                                    <tr>
                                        <td><?= $v['type'] ?></td>
                                        <td><?= $v['price'] ?></td>
                                        <td><?= $v['description'] ?></td>
                                    </tr>
                                <?php } ?>
                                <tr>
                                    <td><b>总计</b></td>
                                    <td><?=$total?></td>
                                    <td><b>成本总计</b></td>
                                </tr>
                            </table>

                        </td>
                    </tr>
                </table>

                <?php } ?>

            </table>
        </div>
        <div class="col-md-8 col-md-offset-2">
            <div class="col-md-12">
                <span>仔猪总成本：<?= $buytotal-$costtotal+$backtotal ?> (仔猪购买费：<?=$buytotal?>；仔猪养殖成本：<?=$costtotal?>；仔猪回购费用：<?=$backtotal?>；<?php if(!$model->isNewRecord){ ?>实际决算价格：<?= $model->finishprice ?><?php } ?>)</span>
            </div>
            <div class="col-md-12">
                    <div style="float: left;margin-right: 20px">应付农户：<?php if($model->isNewRecord) { ?><?= $backtotal-$costtotal ?><?php }else{ ?> <?= $model->finishprice-$costtotal ?> <?php  } ?></div>
                    <div style="float: left;margin-right: 20px">结算人:<?= $model->finishuser?$model->finishuser:'未结算'; ?></div>
                    <div style="float: left;">结算日期:<?= $model->createtime?date('Y-m-d',$model->createtime):'未结算' ?></div>
            </div>

        </div>
        <div class="col-md-2 col-md-offset-10"><button class="btn btn-primary" id="print">打印</button></div>
    </div>
</div>



<script>
    <?php $this->beginBlock('js_end') ?>

    //打印
    $(function(){
        $("#print").printPreview({
            obj2print:'#printarae',
            width:'810'
        });

    });


    <?php $this->endBlock('jsend') ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>

</script>
